package com.topica.pager;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.topica.R;
import com.topica.TopicaApplication;
import com.topica.TopicaCourse;
import com.topica.TopicaQuiz;
import com.topica.adapter.LectureAdapter;
import com.topica.database.callback.OnGetListLectureListener;
import com.topica.database.callback.OnGetQuizListener;
import com.topica.database.task.GetListLectureTask;
import com.topica.database.task.GetQuizTask;
import com.topica.object.LectureItem;
import com.topica.object.QuestionItem;
import com.topica.object.QuizItem;
import com.topica.object.QuizLogItem;
import com.topica.object.WeekItem;

@SuppressLint("ValidFragment")
public class WeekPager extends Fragment implements OnGetListLectureListener,
		OnGetQuizListener {
	public static final String WEEK_ITEM = "com.topica.pager.WeekPager.WEEK_ITEM";
	public static final String WEEK_INDEX = "com.topica.pager.WeekPager.WEEK_INDEX";
	public static final String SELECTED_INDEX = "com.topica.pager.WeekPager.SELECTED_INDEX";
	private WeekItem weekItem;
	private boolean didInit;
	private ListView mListLecture;
	private TextView mTitle;
	private TextView mPractice;
	private TextView mTitleWeek;
	private TextView mLinkWeek;
	private View view;
	private View footer;
	private LectureAdapter lectureAdapter;
	private int index;
	//private String url = "http://mobi.topica.vn/h2472/m/?u=";
	private String url = "http://thuvienso.net/h2472/m/?u=";
	private int indexSelected;


	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.pager_week, null);
		footer = inflater.inflate(R.layout.inc_footer_pager_week, null);
		return view;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		setRetainInstance(true);
		if (!didInit) {
			initVariables();
			didInit = true;
		}
		initControls();
		initActions();
	}

	private void initActions() {
		mListLecture.setAdapter(lectureAdapter);
		mTitleWeek.setText(weekItem.getName());
		mPractice.setText(weekItem.getPractice());
		mTitle.setText(getActivity().getSharedPreferences(TopicaCourse.PREF_COURSE, 0).getString(TopicaCourse.COURSE_NAME_KEY, ""));
		
		if (indexSelected == index && lectureAdapter.isEmpty()) {
			GetListLectureTask lectureTask = new GetListLectureTask(
					getActivity(), "" + weekItem.getWeekId());
			lectureTask.setOnGetListLectureListener(this);
			lectureTask.execute();
		}

		mLinkWeek.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				url = url
						+ ((TopicaApplication) getActivity().getApplication())
								.getUserName();
				Intent intent = new Intent(Intent.ACTION_VIEW);
				intent.setData(Uri.parse(url));
				startActivity(intent);
			}
		});

		mPractice.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				requestQuiz();
			}
		});
	}

	private void initControls() {
		mListLecture = (ListView) view.findViewById(R.id.listLectures);
		mListLecture.addFooterView(footer, null, false);
		mTitle = (TextView) view.findViewById(R.id.txtTitle);
		mTitleWeek = (TextView) view.findViewById(R.id.titleWeek);
		mPractice = (TextView) footer.findViewById(R.id.txtDescPractice);
		mLinkWeek = (TextView) footer.findViewById(R.id.txtLinkQA);
	}

	private void initVariables() {
		weekItem = (WeekItem) getArguments().getSerializable(
				WEEK_ITEM);
		index = getArguments().getInt(WEEK_INDEX);
		indexSelected = getArguments().getInt(SELECTED_INDEX);
		lectureAdapter = new LectureAdapter(getActivity()
				.getApplicationContext(), -1, new ArrayList<LectureItem>());
	}

	public static Fragment newInstance() {
		WeekPager pager = new WeekPager();
		return pager;
	}

	@Override
	public void onGetListLectureListener(ArrayList<LectureItem> data) {
		for (LectureItem item : data) {
			lectureAdapter.add(item);
		}
		lectureAdapter.notifyDataSetChanged();
	}

	public void onPagerChange() {
		if (lectureAdapter.isEmpty()) {
			GetListLectureTask lectureTask = new GetListLectureTask(
					getActivity(), "" + weekItem.getWeekId());
			lectureTask.setOnGetListLectureListener(this);
			lectureTask.execute();
		}
	}

	private void requestQuiz() {
		GetQuizTask getQuizTask = new GetQuizTask(getActivity(), ""
				+ weekItem.getQuizId(), "" + weekItem.getWeekId());
		getQuizTask.setOnGetQuizListener(this);
		getQuizTask.execute();
	}

	@Override
	public void onGetQuizListener(QuizItem quizItem,
			ArrayList<QuestionItem> items) {
		Intent intent = new Intent(getActivity(), TopicaQuiz.class);
		intent.putExtra(TopicaQuiz.LIST_QUESTION, items);
		intent.putExtra(TopicaQuiz.QUIZ_ITEM, quizItem);
		intent.putExtra(TopicaQuiz.WEEK_ID, weekItem.getWeekId());
		startActivity(intent);
	}

	@Override
	public void onTimeoutQuize(ArrayList<QuizLogItem> data) {
		String nodata = getActivity().getResources().getString(
				R.string.title_timeout_quiz);
		Toast.makeText(getActivity().getApplicationContext(), nodata,
				Toast.LENGTH_SHORT).show();
	}

	@Override
	public void onNoData() {
		String nodata = getActivity().getResources().getString(
				R.string.title_nodata);
		Toast.makeText(getActivity().getApplicationContext(), nodata,
				Toast.LENGTH_SHORT).show();
	}

	@Override
	public void onBeforeTimeTest() {
		String data = getActivity().getResources().getString(
				R.string.title_before_time_test);
		Toast.makeText(getActivity().getApplicationContext(), data,
				Toast.LENGTH_SHORT).show();
	}

	@Override
	public void onAfterTimeTest() {
		String data = getActivity().getResources().getString(
				R.string.title_after_time_test);
		Toast.makeText(getActivity().getApplicationContext(), data,
				Toast.LENGTH_SHORT).show();
	}
}
